package com.atguigu.srb.sms.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.atguigu.common.exception.Assert;
import com.atguigu.common.exception.BusinessException;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.sms.service.SmsService;
import com.atguigu.srb.sms.utils.SmsProperties;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author gcq
 * @Create 2021-04-24
 */
@Slf4j
@Service
public class SmsServiceImpl implements SmsService {

    /**
     * 发送短信
     * @param mobile 手机号
     * @param tempalteCode 短信模板
     * @param param 参数
     */
    @Override
    public void send(String mobile, String tempalteCode, Map<String, Object> param) {
        DefaultProfile profile = DefaultProfile.getProfile(
                SmsProperties.REGION_Id,
                SmsProperties.KEY_ID,
                SmsProperties.KEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        // 创建远程请求参数
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        // 远程服务器地址
        request.setSysDomain("dysmsapi.aliyuncs.com");
        // api版本号
        request.setSysVersion("2017-05-25");
        // 远程请求要做什么
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", SmsProperties.REGION_Id);
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", SmsProperties.SIGN_NAME);
        request.putQueryParameter("TemplateCode", tempalteCode);
        
        Gson gson = new Gson();
        String jsonParam = gson.toJson(param);
        request.putQueryParameter("TemplateParam", jsonParam);

        try {
            // 携带客户端请求参数向远程阿里云服务器发起远程调用，并得到响应结果
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());

            // 通信失败处理
            boolean success = response.getHttpResponse().isSuccess();
            Assert.isTrue(success, ResponseEnum.ALIYUN_RESPONSE_ERROR);

            // 获取响应结果
            String data = response.getData();
            HashMap<String, String> resultHashMap = gson.fromJson(data, HashMap.class);
            String code = resultHashMap.get("Code");
            String message = resultHashMap.get("Message");
            log.info("code:" + code + "message:" + message);

            // 业务失败处理
            Assert.notEquals("isv.BUSINESS_LIMIT_CONTROL", code, ResponseEnum.ALIYUN_SMS_LIMIT_CONTROL_ERROR);
            Assert.equals("OK", code, ResponseEnum.ALIYUN_SMS_ERROR);

        } catch (ServerException e) {
            log.info("阿里云短信发送sdk调用失败:" + e.getErrCode() + "," + e.getErrMsg());
            // 抛出异常
            throw new BusinessException(ResponseEnum.ALIYUN_SMS_ERROR, e);
        } catch (ClientException e) {
            log.info("阿里云短信发送sdk调用失败:" + e.getErrCode() + "," + e.getErrMsg());
            // 抛出异常
            throw new BusinessException(ResponseEnum.ALIYUN_SMS_ERROR, e);
        }
    }
}